/**
 * FILE:        Quiz.java
 * PROGRAMMER:  Mark Fulton
 * CLASS:       CSCI 3033
 * DUE DATE:    12/9/2009
 * INSTRUCTOR:  Dr. Hankins
 */

package edu.mtsu.csci3033.quiz;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import edu.mtsu.csci3033.ImageIcons;

/**
 * @author Mark Fulton
 *
 */
public class Quiz extends JPanel implements ActionListener {

    private JLabel questionJLabel = new JLabel();
    private JPanel choicesJPanel = new JPanel();
    private JButton continueJButton = new JButton("Continue", ImageIcons.FORWARD);

    private ArrayList<Question> questionsArrayList = null;
    private Iterator<Question> questionsIterator = null;
    
    private ArrayList<Question> retakeQuestionsArrayList = null;
    private Question currentQuestion = null;
    private ImageIcon correctAnswer = null;
    private ImageIcon currentAnswer = null;

    public Quiz() {
        choicesJPanel.setLayout(new BoxLayout(choicesJPanel, BoxLayout.Y_AXIS));
        
        continueJButton.setEnabled(false);
        continueJButton.addActionListener(this);
        
        setLayout(new BorderLayout(5, 5));
        add(questionJLabel, BorderLayout.NORTH);
        add(choicesJPanel, BorderLayout.CENTER);
        add(continueJButton, BorderLayout.SOUTH);
        
        retakeQuestionsArrayList = new ArrayList<Question>();
    }

    public Quiz(Question[] questions) {
        this();
        setQuestions(questions);
        
        if (questionsIterator.hasNext()) loadNextQuestion();
    }

    private void loadNextQuestion() {
        currentQuestion = questionsIterator.next();
        
        questionJLabel.setIcon(currentQuestion.getQuestion());
        
        correctAnswer = currentQuestion.getAnswer();
        
        choicesJPanel.removeAll();
        ButtonGroup choicesButtonGroup = new ButtonGroup();
        JToggleButton temp = null;
        for (ImageIcon choice : currentQuestion.getChoices()) {
            temp = new JToggleButton(choice);
            choicesJPanel.add(temp);
            choicesButtonGroup.add(temp);
            temp.addActionListener(this);
        }
        repaint();
    }

    /**
     * @param questions the questions to set
     */
    public void setQuestions(Question[] questions) {
        questionsArrayList = new ArrayList<Question>(questions.length);
        for (Question question : questions)
            questionsArrayList.add(question);
        
        questionsIterator = questionsArrayList.iterator();
    }

    /**
     * @return the questions
     */
    public Question[] getQuestions() {
        return (Question[]) questionsArrayList.toArray();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == continueJButton) {
            if (currentAnswer != correctAnswer)
                retakeQuestionsArrayList.add(currentQuestion);
            
            if (questionsIterator.hasNext()) loadNextQuestion();
        } else if (e.getSource() instanceof JToggleButton) {
            continueJButton.setEnabled(true);
            currentAnswer = (ImageIcon) ((JToggleButton)e.getSource()).getIcon();
        }
    }

}
